/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.delivery.multishop.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.util.BmapPoint;
import com.yami.shop.delivery.comment.model.SameCity;
import com.yami.shop.delivery.comment.service.SameCityService;
import com.yami.shop.security.multishop.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;


/**
 *
 *
 * @author lhd
 * @date 2020-05-23 08:26:25
 */
@RestController
@AllArgsConstructor
@RequestMapping("/delivery/sameCity" )
public class SameCityController {

    private final SameCityService sameCityService;

    /**
     * 获取同城配送数据
     * @return 获取同城配送数据
     */
    @GetMapping("/getSameCityInfo" )
    public ResponseEntity<SameCity> getSameCityPage() {
        SameCity sameCity = sameCityService.getSameCityByShopId(SecurityUtils.getShopUser().getShopId());
        if(Objects.nonNull(sameCity)) {
            JSONArray jsonArray = JSONUtil.parseArray(sameCity.getPositionIds());
            sameCity.setPolygonPath(jsonArray.toList(BmapPoint.class));
        }
        return ResponseEntity.ok(sameCity);
    }


    /**
     * 新增
     * @param sameCity 配送数据
     * @return 是否新增成功
     */
    @SysLog("新增" )
    @PostMapping
    public ResponseEntity<Boolean> save(@RequestBody @Valid SameCity sameCity) {
        SameCity sameCityDb = sameCityService.getOne(new LambdaQueryWrapper<SameCity>().eq(SameCity::getShopId, SecurityUtils.getShopUser().getShopId()));
        JSONArray jsonArray = new JSONArray(sameCity.getPolygonPath());
        sameCity.setPositionIds(jsonArray.toString());
        sameCity.setShopId(SecurityUtils.getShopUser().getShopId());
        if(Objects.nonNull(sameCityDb)){
            sameCityService.updateById(sameCity);
        }else{
            sameCityService.save(sameCity);
        }
        sameCityService.removeSameCityByShopId(SecurityUtils.getShopUser().getShopId());
        return ResponseEntity.ok().build();
    }


    /**
     * 修改
     * @param sameCity 配送数据
     * @return 是否修改成功
     */
    @SysLog("修改" )
    @PutMapping
    public ResponseEntity<Boolean> updateById(@RequestBody @Valid SameCity sameCity) {
        JSONArray jsonArray = new JSONArray(sameCity.getPolygonPath());
        sameCity.setPositionIds(jsonArray.toString());
        sameCity.setShopId(SecurityUtils.getShopUser().getShopId());
        boolean flag = sameCityService.updateById(sameCity);
        sameCityService.removeSameCityByShopId(SecurityUtils.getShopUser().getShopId());
        return ResponseEntity.ok(flag);
    }

    /**
     * 通过id删除
     * @param samecityId id
     * @return 是否删除成功
     */
    @SysLog("删除" )
    @DeleteMapping("/{samecityId}" )
    public ResponseEntity<Boolean> removeById(@PathVariable Long samecityId) {
        boolean flag = sameCityService.removeById(samecityId);
        sameCityService.removeSameCityByShopId(SecurityUtils.getShopUser().getShopId());
        return ResponseEntity.ok(flag);
    }

}
